function Toast(msg, duration) {
    duration = isNaN(duration) ? 2000 : duration;
    var m = document.createElement('div');
    m.innerHTML = msg;
    m.style.cssText = "min-width: 15rem; max-width:22rem; opacity: 0.7;\
    color: rgb(255, 255, 255); padding:1.2rem 1.5rem 1.2rem 1.5rem;\
    text-align: center;border-radius: 0.25rem;position: fixed;\
    z-index: 999999;\
    background: rgb(0, 0, 0);font-size: 1.5rem;";
    document.body.appendChild(m);

    m.style.left = (document.documentElement.clientWidth - m.clientWidth) * 0.5 + "px";
    m.style.top = document.documentElement.clientHeight * 0.35 + "px";

    setTimeout(function () {
        var d = 0.5;
        m.style.webkitTransition = '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in';
        m.style.opacity = '0';
        setTimeout(function () { document.body.removeChild(m) }, d * 1000);
    }, duration);
}

$(document).ready(
    function () {
        document.addEventListener('dragover', function (event) {
            event.preventDefault();
            return false;
        }, false);

        document.addEventListener('drop', function (event) {
            event.preventDefault();
            return false;
        }, false);

        Halo.Config.bEditor = true;

        window.halo_func_params = {};
        window.halo_func_ui = {};

        if (window.editor_type === 'desktop') {
            initProject(function () {
                Editor.Signal_Project.dispatch();
            });
        } else {
            window.halo_i18n = halo_lang.CN;
            Halo.init(null, "/assets/");
            Halo.Storage.init('HALOEDITOR');
            var cfg = Halo.Config.get();
            Object.assign(cfg, {
                version: '1.2',
                alpha: false,
                antialias: false,
                preserveDrawingBuffer: false,
                shadowMapType: 1,
                design_size: { x: 1920, y: 1080 },
                textures: {},
                effects: {},
                widgets: {},
                audios: {},
                dragonbones: [],
                batches: {}
            });
        }

        window.viewport = new Viewport();

        // if (window.editor_type === 'desktop') {
        //     Editor.setTheme(ipcRenderer.sendSync('theme'));
        // } else {
        //     Editor.setTheme('light');
        // }

        function animate() {
            requestAnimationFrame(animate);
            window.viewport.render();
        };

        animate();

        document.addEventListener('keydown', function (event) {
            switch (event.keyCode) {
                case 46: // delete
                    break;
            }
        });

        function getQueryString(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return decodeURI(r[2]); return null;
        }

        if (window.editor_type === 'web') {
            window.asset_url = getQueryString('url');
            window.asset_id = getQueryString('id');

            if (window.asset_url && window.asset_id) {
                Halo.httpRequest(window.asset_url, 'arraybuffer').then(function (data) {
                    var meta = Halo.ResourceManager.unzip(data);
                    Halo.ResourceManager.clearCache();
                    Halo.parseProject(meta, function() {
                        Editor.Signal_Project.dispatch();
                    });
                }).catch(function () {
                    alert('request asset error');
                });
            }

            // function success(data) {
            //     var meta = Halo.ResourceManager.unzip(data);
            //     var scn = Halo.parseProject(meta);
            //     if (scn) {
            //         Editor.Signal_SelectObject.dispatch(null);
            //         Editor.Signal_AddObject.dispatch(scn, null);
            //     }
            // };

            // var request = new XMLHttpRequest();
            // request.onreadystatechange = function () {
            //     if (request.readyState === 4) {
            //         if (request.status === 200) {
            //             if (request.response !== 'empty') {
            //                 Editor.Signal_UpdateProgress.dispatch('Loading: Finish');
            //                 success(request.response);
            //             }
            //         }
            //     }
            // }
            // request.onprogress = function (evt) {
            //     if (evt.lengthComputable) {
            //         Editor.Signal_UpdateProgress.dispatch('Loading: ' + evt.loaded + '/' + evt.total);
            //     }
            // };
            // request.open('GET', '/get_edit_asset', true);
            // request.responseType = 'arraybuffer';
            // request.send();
        }
    }
);
